package com.yuanhua.xcxmng.wechat.controller;

import java.util.Date;
import java.util.List;

import javax.annotation.Resource;

import com.yuanhua.xcxmng.wechat.service.UserDeviceService;
import org.springframework.web.bind.annotation.*;

import com.google.common.collect.Lists;
import com.yuanhua.xcxmng.wechat.entity.HistoryTable;
import com.yuanhua.xcxmng.wechat.entity.User;
import com.yuanhua.xcxmng.wechat.entity.UserIndicator;
import com.yuanhua.xcxmng.wechat.entity.WechatResult;
import com.yuanhua.xcxmng.wechat.exception.SessionException;
import com.yuanhua.xcxmng.wechat.service.IndicatorService;

import cn.hutool.core.date.DateTime;
import cn.hutool.core.date.DateUtil;
import lombok.extern.slf4j.Slf4j;

/**
 * The type Indicator controller.
 *
 * @author hzy24985
 * @version $Id : IndicatorController, v 0.1 2018/8/5 下午4:15 hzy24985 Exp $
 */
@RestController
@RequestMapping("/wechat/indicator")
@Slf4j
public class IndicatorController extends BaseController {

    @Resource
    private IndicatorService indicatorService;
    @Resource
    private UserDeviceService userDeviceService;

    /**
     * 保存体脂数据
     *
     * @param indicator 体脂数据.
     * @return 是否成功. wechat result
     * @throws SessionException the session exception
     */
    @PostMapping("save")
    public WechatResult<String> save(@RequestBody UserIndicator indicator) throws SessionException {
        User user = getUser();
        int id = indicatorService.save(indicator, user);

        log.info("插入数据，id:{}", id);
        return WechatResult.success();
    }

    /**
     * 最近的历史数据.
     * index = 5时，只展示最近3次。其他展示当月每天最后一次测量的数据.
     *
     * @param index 屏幕索引。
     * @return 历史数据. last 3 records
     * @throws SessionException session超时.
     */
    @GetMapping("lastRecords")
    public WechatResult<List<UserIndicator>> getLastRecords(int index) throws SessionException {
        String openId = getOpenId();
        List<Date> latest5Month = getLatest5Month();
        Date now = latest5Month.get(index);
        DateTime startOfMonth = DateUtil.beginOfMonth(now);
        DateTime endOfMonth = DateUtil.endOfMonth(now);
        List<UserIndicator> list = Lists.newArrayList();
        if (index == 5) {
            list = indicatorService.getLatestRecord(openId, startOfMonth, endOfMonth, 3);
        } else {
            list = indicatorService.getIndicatorReport(getOpenId(), startOfMonth, endOfMonth);
        }
        return WechatResult.success(list);
    }

    /**
     * 历史数据和周数据
     *
     * @param index 界面索引
     * @return history table data
     * @throws SessionException the session exception
     */
    @GetMapping("history")
    public WechatResult<List<HistoryTable>> getHistoryTableData(int index) throws SessionException {
        return WechatResult.success(indicatorService.getHistoryTableData(getOpenId(), index, getLatest5Month()));
    }

    /**
     * 解除与设备的绑定.
     *
     * @param deviceId the device id
     * @return wechat result
     * @throws SessionException the session exception
     */
    @PostMapping("relieve")
    public WechatResult<String> relieveLink(String deviceId) throws SessionException {
        userDeviceService.relieveLink(deviceId, getUser());
        return WechatResult.success();
    }

    /**
     * 最近5个月.
     * @return 最近5个月.
     */
    private List<Date> getLatest5Month() {
        Date current = new Date();
        List<Date> list = Lists.newArrayList();
        list.add(DateUtil.offsetMonth(current, -5));
        list.add(DateUtil.offsetMonth(current, -4));
        list.add(DateUtil.offsetMonth(current, -3));
        list.add(DateUtil.offsetMonth(current, -2));
        list.add(DateUtil.offsetMonth(current, -1));
        list.add(current);
        return list;
    }
}
